require(['../config/requireconfig'],function(config){
	require(['jquery','common/pccommons','common/banner','common/validate'],function($,com,banner,validate){
		com.init({headerPos:{cur:"index"}, lazyHeader:true});
		 var pgLogic = new PageLogic($,com,banner);
			pgLogic.init();
	});
});
var PageLogic = function($,com,banner){
	return {
		init:function(){
			var _this = this;
			//_this.banner();
			$("#loginForm").inputForm();
			// 事件绑定
			_this.bindEvents();
		},
		//banner
		banner:function(){
			com.ajax({
				url:'/pc/home/banners.ahtml',
				type:'get',
				data:{"systemDiv":18 , "type" :2 ,  "bannerArea":2}, 
				success:function(data){
					var array=new Array(); 
					$(data.data).each(function(idx,ban){
						var map = {} 		 			 
						map.imgPath = ban.imageUrl;  
						map.url = ban.url;
						array.push(map);
				 	});
					banner.init({
						container:$(".banner"),
						data:array
					});
				}
			});
		},
		// 事件绑定
		bindEvents:function(){
			var _this = this;
			$("#imageAuthCode").on("click",function(){
				var val = $("#token").val();
				if(!val){
					showImg();
				}
			});

			$(".register-yzm-img").on("click",function(){
				showImg();
			});

			function showImg(){
				$(".register-yzm-img").show();
				com.ajax({
					url:"/kaptchaNoSession.vjpg",
					type:'get',
					success:function(res){
						$(".register-yzm-img").prop("src",res.png);
						$("#token").val(res.token);
					}
				})
			}

			//按钮提交
			$("#doSubmit").on("click", function(){
					if (!$("#phone").validate() || !$("#password").validate() || !$("#imageAuthCode").validate()) {
						return;
					}
					com.ajax({
						url:"/login.json",
						type:"POST",
						dataType:"json",
						data:{"username":$("#phone").val(),
							  "password":$("#password").val(),
							  "captcha":$("#imageAuthCode").val(),
							  "token":$("#token").val()
							},
						success:function(data){
							if (data.success == true) {
                                var redirectUrl = com.utils().urlParam("redirectUrl");
                                if(redirectUrl) {
                                	// 如果需要跳转回原页面地址，重新定向一次
                                    com.utils().windowJump(redirectUrl, false);
                                    return;
								}
                                com.utils().windowJump("../self/myMessage.html", false);
							} else {
								_this.showMessage(data);
								//alert(data.message);
							}
						},
						error:function(){
						}
					});
			});
		},
		// 登录message显示
		showMessage:function(data){
			var msgObj = "";
		
			switch(data.messageCode){
				case "E0200009":{ // 帐号异常
					msgObj = "phone";
					break;
				}
				case "E0200010":{ // 密码相关message
					msgObj = "password";
					break;
				}
				case "E0200005":
				case "E0200006":{ // 图像验证码相关message
					msgObj = "imageAuthCode";
					break;
				}
				case "E0200011":
				case "E0200008":{// 注册时其他相关message
					msgObj = "common";
					break;
				}
			}
			if($.trim(data.message).length > 0) {
				var messageId = "#"+msgObj+"_message";
				$(messageId).html("").html(data.message)
			}
		},
	};
};
